﻿using System;
using MyLibs;


namespace MyLabs
{
    class Program
    {
        public static void Main()
        {
            //BaseCalc();
            //ExecReverseNum();
            //CountBin();
            //ExecReverseStr();
            //CompareTimeSpan();
            //PrintDevidersA();
            //ExecBitCointer();
            //CommonDeviders();
            execGCD();

            Console.ReadKey();
            

        }

        static TimeSpan CountTimeStr(string str)
        {
            DateTime start = DateTime.Now;
            int count = 1000000;
            while (count > 0)
            {
                ReverseStr(str);
                count--;
            }
            DateTime finish = DateTime.Now;
            TimeSpan diff = finish.Subtract(start);
            return diff;
        }

        static TimeSpan CountTimeNum(string str)
        {    
            DateTime start = DateTime.Now;
            int count = 1000000;
            while (count > 0)
            {
                ReverseNum1(Convert.ToInt32(str));
                count--;
            }
            DateTime finish = DateTime.Now;
            TimeSpan diff = finish.Subtract(start);
            return diff;
        }

        static void CompareTimeSpan()
        {
            Console.WriteLine("Введите число:");
            string str = Console.ReadLine();
            Console.WriteLine("{0} - подсчет методами Int;", CountTimeNum(str));
            Console.WriteLine("{0} - подсчет методами String;", CountTimeStr(str));
            Console.ReadKey();

        }

        
        public static void BaseCalc()
        {
     
            float a = Helpers.ReadIntFromConsole();
            float b = Helpers.ReadIntFromConsole();  
         
            if (b == 0)
            {
                Console.WriteLine("Сумма: {0}; Разность: {1}; Произведение: {2}."
                , a + b, a - b, a * b);
                Console.WriteLine("Внимание! Деление на 0.");
            }
            else {
                Console.WriteLine("Сумма: {0}; Разность: {1}; Произведение: {2}; Деление: {3}."
                , a + b, a - b, a * b, a / b);
            }
            Console.ReadKey();
        }

        static void ExecReverseNum()
        {
            int a = Helpers.ReadIntFromConsole();
            int c;
            int b = ReverseNum(a,out c);
            //b.ToString("D10");
            Console.WriteLine(b.ToString("D" + c));
            Console.ReadKey();
        }

        static int ReverseNum( int a, out int c)
        {
            int b = 0;
            c = 0;
            while (a > 0)
            {
                b = b * 10 + a % 10;
                a /= 10;
                c++;
            }
            return b;
        }

        static int ReverseNum1(int a)
        {
            int b = 0;
            //c = 0;
            while (a > 0)
            {
                b = b * 10 + a % 10;
                a /= 10;
            }
            return b;
        }

        static string ReverseStr(string str)
        {
            string str1 = "";
            for (int chr = str.Length - 1; chr >=0 ; chr-- )
            {
                str1 = str1 + str[chr];
            }
            return str1;
        }

        static void PrintDeviders()
        {
            int num = Helpers.ReadIntFromConsole();

            Console.WriteLine("Делители числа {0}: {1}", num, DefineDeviders(num));
            Console.ReadKey();
        }


        static void PrintDevidersA()
        {
            int num = Helpers.ReadIntFromConsole();
            int[] divsArrray = DefineDevidersA(num);
            for (int i = 0; i < divsArrray.Length; i++ )
            { Console.Write("{0} ", divsArrray[i]); }
            
            Console.ReadKey();
        }
        //Делители числа 
        static string DefineDeviders(int num)
        {
            string div = "";
            for (int i = 1; i <= num; i++)
            {
                if (num % i == 0)
                {
                    div = div + i + "; ";
                }
            }
            return div;
        }

        //Делители числа массив
        static int[] DefineDevidersA(int num)
        {
            int[] div = new int[100];
            int j = 0;
            for (int i = 1; i <= num; i++)
            {
                
                if (num % i == 0)
                {
                    div[j] = i;
                }
                j++;
            }
            return div;
        }

        static void CommonDeviders()
        {
            Console.WriteLine("Сколько будет чисел?");
            int N = int.Parse(Console.ReadLine());
            int[] numbers = new int[100];

            int[][] num = new int[100][];
            int i1 = 0;
            for (int i = 0; i <= num.Length; i++)
            {
                num[i] = new int[100];
                for (int j = 0; j < num[i].Length; j++)
                {
                    num[j] = DefineDevidersA(numbers[i1]);
                }
                i1++;
            }

        for (int i = 0; i < num.Length; i++ )
            for (int j = 1; j < num[i].Length; j++)
            {
                Console.WriteLine(num[i][j]);
            }
        
        }
        

        static void ExecReverseStr()
        {
            Console.WriteLine("Введите строку для преобразования:");
            string str = Console.ReadLine();
            Console.WriteLine(ReverseStr(str));
            Console.ReadKey();
        }
        
        //calc positive digits of bin
        public static void CountBin()
    {
        int a = Helpers.ReadIntFromConsole();
        int b = a;
        string forTest = Convert.ToString(a, 2);
        int n = 0;
        while (a > 0)
        {
            if (a % 2 == 1) { n++; }
            a /= 2;
        }
        Console.WriteLine("dec {0} = bin {1}", b, forTest);
        Console.WriteLine( n );
        Console.ReadKey();
    }

        static int BitCounter(int N)
        {
            int count = 0;

            for (int i = 0; i < 32; i++)
            {
                count += N & 1;
                N = N >> 1;
            }
            return count;
        }

        static void ExecBitCointer()
        {
            Console.WriteLine("Введите число для подсчета");
            int a = int.Parse(Console.ReadLine());

            Console.WriteLine(BitCounter(a));
        }

        static int GCD(int a, int b)
        {
            int Remainder;

            while (b != 0)
            {
                Remainder = a % b;
                a = b;
                b = Remainder;
            }
            return a;
        }
        static int GCD1(int a, int b)
        {
            while (a !=b)
            {
                if (a > b)
                {
                    a = a - b;
                }
                else
                {
                    b = b - a;
                }
            }
            return a;
        }
        static void execGCD(){

            Console.WriteLine("This program allows calculating the GCD");
            Console.Write("Value 1: ");
            int x = Helpers.ReadIntFromConsole();
            Console.Write("Value 2: ");
            int y = Helpers.ReadIntFromConsole();

            Console.Write("\nThe Greatest Common Divisor of ");
            Console.WriteLine("{0} and {1} is {2}", x, y, GCD1(x, y));
    }          
    }
}